<template>
  <!-- 库存与销量图表组件 -->
  <div class="com-container">
    <div class="com-chart" ref="stock"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  data() {
    return {
      // echarts对象
      myChart: null,
      // 数据
      stockData: [],
      // 当前显示数据的页数
      currentIndex: 0,
      // 定时器
      timer: null
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      this.myChart.dispose()
      this.initChart()
      this.updateChart()
      this.screenAdapter()
    }
  },
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdapter)
    this.screenAdapter()
    this.startInterval()
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdapter)
    if (this.timer) clearInterval(this.timer)
    this.myChart.off('mouseover')
    this.myChart.off('mouseout')
  },
  methods: {
    // 初始化echarts对象
    initChart() {
      this.myChart = this.$echarts.init(this.$refs.stock, this.theme)
      const initOption = {
        title: {
          text: '▎库存与销量分析',
          top: 20,
          left: 20
        }
      }
      this.myChart.setOption(initOption)
      this.myChart.on('mouseover', () => {
        if (this.timer) clearInterval(this.timer)
      })
      this.myChart.on('mouseout', this.startInterval)
    },
    // 获取数据
    async getData() {
      // 处理数据后赋值给stockData
      const res = (this.stockData = await this.$getApi.getStock())
      this.updateChart()
    },
    // 更新图表
    updateChart() {
      const centerArr = [
        ['18%', '40%'],
        ['50%', '40%'],
        ['82%', '40%'],
        ['34%', '75%'],
        ['66%', '75%']
      ]
      const colorArr = [
        ['#4FF778', '#0BA82C'],
        ['#E5DD45', '#E8B11C'],
        ['#E8821C', '#E55445'],
        ['#5052EE', '#AB6EE5'],
        ['#23E5E5', '#2E72BF']
      ]
      const start = this.currentIndex * 5
      const end = this.currentIndex * 5 + 5
      const showData = this.stockData.slice(start, end)
      const seriesData = showData.map((item, index) => {
        return {
          type: 'pie',
          center: centerArr[index],
          hoverAnimation: false,
          label: {
            position: 'center',
            color: colorArr[index][0]
          },
          labelLine: false,
          data: [
            {
              // item.name + '\n' + item.sales
              name: `${item.name}\n销量：${item.sales}\n库存：${item.stock}
              `,
              value: item.sales,
              itemStyle: {
                color: new this.$echarts.graphic.LinearGradient(0, 1, 0, 0, [
                  {
                    offset: 0,
                    color: colorArr[index][0]
                  },
                  {
                    offset: 1,
                    color: colorArr[index][1]
                  }
                ])
              }
            },
            {
              value: item.stock,
              itemStyle: {
                color: '#333843'
              }
            }
          ]
        }
      })
      const dataOption = {
        series: seriesData
      }
      this.myChart.setOption(dataOption)
    },
    // 图表适配浏览器窗口大小变化
    screenAdapter() {
      const titleFontSize = (this.$refs.stock.offsetWidth / 100) * 3.6
      const adaptOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize
          }
        },
        series: [
          {
            type: 'pie',
            label: {
              fontSize: titleFontSize / 1.5,
              lineHeight: titleFontSize
            },
            radius: [titleFontSize * 2.8, titleFontSize * 3.2]
          },
          {
            type: 'pie',
            label: {
              fontSize: titleFontSize / 1.5,
              lineHeight: titleFontSize
            },
            radius: [titleFontSize * 2.8, titleFontSize * 3.2]
          },
          {
            type: 'pie',
            label: {
              fontSize: titleFontSize / 1.5,
              lineHeight: titleFontSize
            },
            radius: [titleFontSize * 2.8, titleFontSize * 3.2]
          },
          {
            type: 'pie',
            label: {
              fontSize: titleFontSize / 1.5,
              lineHeight: titleFontSize
            },
            radius: [titleFontSize * 2.8, titleFontSize * 3.2]
          },
          {
            type: 'pie',
            label: {
              fontSize: titleFontSize / 1.5,
              lineHeight: titleFontSize
            },
            radius: [titleFontSize * 2.8, titleFontSize * 3.2]
          }
        ]
      }
      this.myChart.setOption(adaptOption)
      this.myChart.resize()
    },
    // 定时器
    startInterval() {
      if (this.timer) clearInterval(this.timer)
      this.timer = setInterval(() => {
        this.currentIndex++
        let totalPage = Math.ceil(this.stockData.length / 5)
        if (this.currentIndex > totalPage - 1) this.currentIndex = 0
        this.updateChart()
      }, 3000)
    }
  }
}
</script>

<style>
</style>
